﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PictrueSimpleSyndication
{
    public partial class FormPSSList : Form
    {
        public FormPSSList()
        {
            InitializeComponent();
        }

        private void FormPSSList_Load(object sender, EventArgs e)
        {
            IDataSubscribeList subscribe = SingleCase.GetInstance().GetRegister<IDataSubscribeList>();
            if (subscribe != null)
            {
                var listparent = subscribe.GetRootSubscribeInfo();
                foreach (var item in listparent ?? new List<SubscribeInfo>())
                {

                    var root = new TreeNode
                    {
                        Name = item.Identification,
                        Text = item.Title,
                    };
                    treeView1.Nodes.Add(root);

                    foreach (var child in subscribe.GetAllChildSubscribeInfo(item.Identification) ?? new List<SubscribeInfo>())
                    {
                        root.Nodes.Add(new TreeNode
                        {
                            Name = child.Identification,
                            Text = child.Title,
                        });
                    }
                }
            }
        }

        /// <summary>
        /// 数据提供其
        /// </summary>
        public IDataContentList DataContentList { get; set; }

        private void UpdateRightItemWidth()
        {
            foreach (Control item in flowLayoutPanel1.Controls)
            {
                item.Width = flowLayoutPanel1.Width - 50;
            }
        }

        /// <summary>
        /// 更新右侧的界面
        /// </summary>
        public void UpdateRightView()
        {
            flowLayoutPanel1.Controls.Clear();

            if (DataContentList != null)
            {
                var list = DataContentList.GetContentInfo();
                if (list != null)
                {
                    foreach (var item in list)
                    {
                        var title_control = new UserControlContentItemTitle();
                        title_control.SetContentItemInfo(item);
                        title_control.Identification = item.Identification;
                        title_control.DataContent = DataContentList.GetDataContent(item.Identification);

                        title_control.DoubleClick += Title_control_DoubleClick;

                        flowLayoutPanel1.Controls.Add(title_control);
                    }
                }

                UpdateRightItemWidth();
            }
        }

        private void Title_control_DoubleClick(object sender, EventArgs e)
        {
            if (!(sender is UserControlContentItemTitle))
                return;

            UserControlContentItemTitle usercontrol = sender as UserControlContentItemTitle;

            if (!string.IsNullOrEmpty(usercontrol.Identification) && usercontrol.DataContent != null)
            {
                var form = new FormPSS();
                form.DataContent = DataContentList.GetDataContent(usercontrol.Identification);
                form.Show();
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DataContentList = SingleCase.GetInstance().GetRegister<IDataSubscribeList>()?.GetDataContentList(e.Node.Name);
            UpdateRightView();
        }

        private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
        {
            UpdateRightItemWidth();
        }

        private void flowLayoutPanel1_Resize(object sender, EventArgs e)
        {
            UpdateRightItemWidth();
        }
    }
}
